package com.hj.chartdemo.utils

import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import com.hj.chartdemo.BR
import com.hj.chartdemo.viewmodel.BaseViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * @author Michael
 * @date  2018/11/14 - 14:52.
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {
    lateinit var binding: T
    private lateinit var viewModel: ViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ScreenAdaptUtil.setCustomDensity(this, application)

        binding = DataBindingUtil.setContentView(this, initContentView())
        viewModel = if (initViewModel() == null) {
            ViewModelProviders.of(this).get(BaseViewModel::class.java)
        } else {
            initViewModel()!!
        }
        binding.setVariable(initVariableId(), viewModel)
        binding.setLifecycleOwner(this)

        initActivity()

        initView()

        initData()
    }

    fun MyToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }

    private fun initActivity() {
        if (BaseViewModel::class.java.isInstance(viewModel)) {
            var baseViewModel = viewModel as BaseViewModel
            //公用Toast 用于消息提示
            baseViewModel.toast.observe(this, Observer {
                Log.e("acitivityToast", "$it:$this")
                MyToast(it)
            })
            //跳转
            baseViewModel.intent.observe(this, Observer {
                it.setClass(this, it.cls)
                startActivity(it)
            })
            //关闭activity
            baseViewModel.finish.observe(this, Observer {
                finish()
            })
            baseViewModel.finishWithBack.observe(this, Observer {
                setResult(it, intent)
                finish()
            })
        }
    }

    /**
     * 返回布局id
     */
    abstract fun initContentView(): Int

    /**
     * 返回ViewMode
     */
    open fun initViewModel(): ViewModel? {
        return null
    }

    /**
     * 返回绑定的VariableId 默认viewMode 可以覆盖重写
     */
    open fun initVariableId(): Int {
        return BR.viewModel
    }

    /**
     * 初始化view
     */
    abstract fun initView()

    /**
     * 初始化data
     */
    abstract fun initData()


    /**
     * 更改字体大小
     */
    protected fun initFontScale() {
        val configuration = resources.configuration
        configuration.fontScale = 1f
        //0.85 小, 1 标准大小, 1.15 大，1.3 超大 ，1.45 特大
        val metrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(metrics)
        metrics.scaledDensity = configuration.fontScale * metrics.density
        resources.updateConfiguration(configuration, metrics)
    }

}
